package cn.stronglink.oil.map;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.utils.CoordinateConverter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.stronglink.oil.R;
import cn.stronglink.oil.entity.DeviceAlarmInfo;
import cn.stronglink.oil.entity.DeviceRealTimeInfo;

/**
 * 地图覆盖物和反地理编码
 * Created by chen on 2017/5/8.
 */

public class MapViewOverlay implements BaiduMap.OnMarkerClickListener {
    private IMapViewOverlyListener mapViewOverlyListener;
    private Activity activity;
    private MapView mapView;
    private BaiduMap mBaiduMap;
//    BitmapDescriptor deviceDb = BitmapDescriptorFactory
//            .fromResource(R.mipmap.device_climate);
    List<BitmapDescriptor> gifList = null;
    Map<Marker, DeviceRealTimeInfo.GasDevicesBean> mapInfo = null;
    Map<Marker, DeviceAlarmInfo> alarmInfoMap = null;
    private static final String DEVICE_KEY = "deviceInfo", ALARM_DEVICE_KEY = "alarmInfo";

    public MapViewOverlay(Activity activity, MapView mapView) {
        this.activity = activity;
        this.mapView = mapView;
        mBaiduMap = mapView.getMap();
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(14.0f);
        mBaiduMap.setMapStatus(mapStatusUpdate);
        gifList = new ArrayList<>();
        mapInfo = new HashMap<>();
        alarmInfoMap = new HashMap<>();
        mBaiduMap.setOnMarkerClickListener(this);
    }

    public IMapViewOverlyListener getMapViewOverlyListener() {
        return mapViewOverlyListener;
    }

    public void setMapViewOverlyListener(IMapViewOverlyListener mapViewOverlyListener) {
        this.mapViewOverlyListener = mapViewOverlyListener;
    }

    public void setOverlay(DeviceRealTimeInfo.GasDevicesBean deviceInfo) {
        LatLng latLng = new LatLng(deviceInfo.getDeviceLng(), deviceInfo.getDeviceLat());
        CoordinateConverter converter = new CoordinateConverter();
        converter.from(CoordinateConverter.CoordType.COMMON);
        converter.coord(latLng);
        LatLng desLatLng = converter.convert();

        BitmapDescriptor deviceDb = setDeviceView(deviceInfo);
        //覆盖物动画设置
        MarkerOptions markerOptions = new MarkerOptions().position(desLatLng).icon(deviceDb).zIndex(5).draggable(true);
        markerOptions.animateType(MarkerOptions.MarkerAnimateType.grow);
        Marker deviceMarker = (Marker) mBaiduMap.addOverlay(markerOptions);
        Bundle bundle = new Bundle();
        bundle.putSerializable(DEVICE_KEY, deviceInfo);
        deviceMarker.setExtraInfo(bundle);
        gifList.add(deviceDb);
        mapInfo.put(deviceMarker, deviceInfo);
        //将地图显示在marker的位置
        MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(desLatLng);
        mBaiduMap.setMapStatus(msu);
    }

    private BitmapDescriptor setDeviceView(DeviceRealTimeInfo.GasDevicesBean deviceInfo) {
        View view = View.inflate(activity, R.layout.marker_layout_out, null);
        // 填充数据
        TextView tv_device_name = (TextView) view.findViewById(R.id.tv_device_name);
        ImageView iv_device_icon = (ImageView) view.findViewById(R.id.iv_device_icon);
        iv_device_icon.setImageBitmap(BitmapFactory.decodeResource(activity.getResources(), R.mipmap.device_climate));
        tv_device_name.setText(deviceInfo.getDeviceId() + "");
        // 构建BitmapDescriptor
        BitmapDescriptor deviceDb = BitmapDescriptorFactory.fromView(view);
        return deviceDb;
    }

    public void setOverlay(DeviceAlarmInfo deviceAlarmInfo) {
        LatLng latLng = new LatLng(deviceAlarmInfo.getLng(), deviceAlarmInfo.getLat());
        CoordinateConverter converter = new CoordinateConverter();
        converter.from(CoordinateConverter.CoordType.COMMON);
        converter.coord(latLng);
        LatLng desLatLng = converter.convert();
        BitmapDescriptor deviceDb =setAlarmDeviceView(deviceAlarmInfo);
        //覆盖物动画设置
        MarkerOptions markerOptions = new MarkerOptions().position(desLatLng).icon(deviceDb).zIndex(5).draggable(true);
        markerOptions.animateType(MarkerOptions.MarkerAnimateType.grow);
        Marker deviceAlarmMarker = (Marker) mBaiduMap.addOverlay(markerOptions);
        Bundle bundle = new Bundle();
        bundle.putSerializable(ALARM_DEVICE_KEY, deviceAlarmInfo);
        deviceAlarmMarker.setExtraInfo(bundle);
        gifList.add(deviceDb);
        alarmInfoMap.put(deviceAlarmMarker, deviceAlarmInfo);

        //将地图显示在marker的位置
        MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(desLatLng);
        mBaiduMap.setMapStatus(msu);
    }

    private BitmapDescriptor setAlarmDeviceView(DeviceAlarmInfo deviceAlarmInfo) {
        View view = View.inflate(activity, R.layout.marker_layout_out, null);
        // 填充数据
        TextView tv_device_name = (TextView) view.findViewById(R.id.tv_device_name);
        ImageView iv_device_icon = (ImageView) view.findViewById(R.id.iv_device_icon);
        iv_device_icon.setImageBitmap(BitmapFactory.decodeResource(activity.getResources(), R.mipmap.device_alarm));
        tv_device_name.setText(deviceAlarmInfo.getDeviceId() + "");
        // 构建BitmapDescriptor
        BitmapDescriptor deviceDb  = BitmapDescriptorFactory.fromView(view);
        return deviceDb;
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        Bundle bundle = marker.getExtraInfo();
        DeviceRealTimeInfo.GasDevicesBean deviceInfo = (DeviceRealTimeInfo.GasDevicesBean) bundle.getSerializable(DEVICE_KEY);
        DeviceAlarmInfo  alarmInfo = (DeviceAlarmInfo) bundle.getSerializable(ALARM_DEVICE_KEY);
        if (deviceInfo != null) {
            mapViewOverlyListener.onClickDevice(deviceInfo);
        }

        if (alarmInfo != null) {
            mapViewOverlyListener.onClickAlarmDevice(alarmInfo);
        }
        return false;
    }

    /**
     * 清除所有Overlay
     */
    public void clearOverlay() {
        mBaiduMap.clear();
        gifList = new ArrayList<>();
        mapInfo.clear();
    }

    public void clearAlarmOverlay(){
        mBaiduMap.clear();
        alarmInfoMap.clear();
    }

}
